//
//  CJExten+Data.swift
//  CJCore
//
//  Created by 陈警卫 on 2024-06-11.
//

import Foundation

public extension Data {
    
    /// 转Dict
    var cj_dict: Any? {
        if JSONSerialization.isValidJSONObject(self) {
            return nil
        }
        let value = try? JSONSerialization.jsonObject(with: self, options: .allowFragments) as? [String: Any]
        return value
    }
    
    /// Data转16进制字符串 默认start/end为全部
    func cj_hexString(start: Int? = nil, end: Int? = nil, space: Bool = false) -> String {
        if self.count <= 0 {
            return ""
        }
        var start = start
        var end = end
        if start == nil { start = 0 }
        if end == nil { end = self.count }
        
        let data = Data(self)
        let bytes = Array<UInt8>(data[start!..<end!])
        let hex = bytes.cj_hexString(space: space)
        return hex
    }
    
    /// Data转ascii字符串 默认start/end为全部
    func cj_asciiString(start: Int? = nil, end: Int? = nil) -> String {
        if self.count <= 0 {
            return ""
        }
        var start = start
        var end = end
        if start == nil { start = 0 }
        if end == nil { end = self.count }
        
        let data = Data(self)
        if let asciiString = String(data: data[start!..<end!], encoding: .ascii) {
            return asciiString
        }
        return ""
    }
    
    /// Data转ascii字符串 correct纠正
    func cj_asciiCorrect() -> String {
        if self.count <= 0 {
            return ""
        }
        let data = Data(self)
        var asciiString = ""
        for byte in data {
            if byte == 0 {
                break
            }
            asciiString = String(format: "%@%@", asciiString, String(format: "%c", byte))
        }
        return asciiString
    }
}


public extension Data {
    
    /// 转Int
    var cj_int: Int {
        var value: Int = 0
        (self as NSData).getBytes(&value, length: cj_sizeof(value))
        return value
    }
    
    /// 转uint
    func cj_uint(_ len: Int = 0) -> UInt {
        let bytes = [UInt8](self)
        
        var lent = len
        lent = lent == 0 ? bytes.count : lent
        lent = lent > bytes.count ? bytes.count : lent
        
        var value: UInt = 0
        for i in 0..<lent {
            value = value + (UInt(bytes[i]) << (i * 8))
        }
        return value
    }
    
    /// 转uint
    func cj_uint8() -> UInt8 {
        return UInt8(cj_uint(1))
    }
    /// 转uint
    func cj_uint16() -> UInt16 {
        return UInt16(cj_uint(2))
    }
    /// 转uint
    func cj_uint32() -> UInt32 {
        return UInt32(cj_uint(4))
    }
    
    /// 转int
    func cj_int(_ len: Int = 0) -> Int {
        let bytes = [UInt8](self)
        var lent = len
        
        lent = lent == 0 ? bytes.count : lent
        lent = lent > bytes.count ? bytes.count : lent
        
        var value: Int = 0
        for i in 0..<lent {
            value = value + (Int(bytes[i]) << (i * 8))
        }
        return value
    }
    
    /// 转int8
    var cj_int8: Int8 {
        return Int8(cj_int(1))
    }
    /// 转int16
    var cj_int16: Int16 {
        return Int16(cj_int(2))
    }
    /// 转int32
    var cj_int32: Int32 {
        return Int32(cj_int(4))
    }
}

public extension Data {
    
    /// 安全的取数据
    func cj_sub(loc: Int, len: Int) -> Data {
        let start = loc
        let end = loc + len
        if end > self.count {
            return Data()
        }
        return Data(self[start...end])
    }
}

